<template>
  <div>
    <el-card
      shadow="always"
      :body-style="{ padding: '20px' }"
      style="margin: 0 auto; width: 50%"
    >
      <div slot="header" style="color: red">
        <h2>欢 迎 注 册</h2>
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名：" prop="userName">
          <el-input
            v-model="ruleForm.userName"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="phone">
          <el-input
            v-model="ruleForm.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密  码：" prop="password">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码：" prop="confirmPassword">
          <el-input
            v-model="ruleForm.confirmPassword"
            placeholder="请输入确认密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >注 册</el-button
          >&nbsp;&nbsp;&nbsp;
          <el-link type="primary" @click="UserLogin"
            >已有账号，立即登录</el-link
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import axios from "@/unit/request";
export default {
  data() {
    return {
      ruleForm: {
        userName: "",
        phone: "",
        password: "",
        confirmPassword: "",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        confirmPassword: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post("/Register/Register", this.ruleForm).then((res) => {
            if (res.data.code > 0) {
              this.$message.error(res.data.message);
            } else {
              this.$message.success(res.data.message);

              this.$router.push("/login");
            }
          });
        }
      });
    },
    UserLogin() {
      this.$router.push("/");
    },
  },
};
</script>
<style >
</style>